//
//  HeadCollectionview.m
//  项目A --半糖
//
//  Created by romana on 16/5/28.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "HeadCollectionview.h"
#import "AFNetworking.h"
#import "ClassifyHeadModel.h"
#import "ThirdTabHeadCollectionViewCell.h"
#import "UIImageView+WebCache.h"

#define WIDTH self.frame.size.width

@implementation HeadCollectionview


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self headcollectionView];
    }
    return self;
}


-(void)headcollectionView{
    UICollectionViewFlowLayout *smalllayout = [[UICollectionViewFlowLayout alloc] init];
    smalllayout.minimumInteritemSpacing = 10;
    smalllayout.minimumLineSpacing = 10;
    smalllayout.itemSize = CGSizeMake((WIDTH - 20) / 3, 70);
    smalllayout.sectionInset = UIEdgeInsetsMake(25, 10, 0, 10);
    smalllayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    BaseCollectionView *smallcollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:smalllayout];
    smallcollectionView.delegate = self;
    smallcollectionView.dataSource = self;
    smallcollectionView.showsHorizontalScrollIndicator = NO;
    smallcollectionView.backgroundColor = [UIColor colorWithRed:234/ 255.0 green:248 / 255.0 blue:254 / 255.0 alpha:1];
    [self addSubview:smallcollectionView];
    [smallcollectionView registerClass:[ThirdTabHeadCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.MyCollectionView = smallcollectionView;
    [self getData];
}

-(void)getData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.liwushuo.com/v2/collections?limit=20&offset=0" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject[@"data"];
        NSArray *arr = dic[@"collections"];
        self.myArray = [NSMutableArray array];
        for (NSDictionary *subDic in arr) {
            ClassifyHeadModel *class = [[ClassifyHeadModel alloc] init];
            [class setValuesForKeysWithDictionary:subDic];
            [self.myArray addObject:class];
            
        }
        [self.MyCollectionView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];






}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.myArray.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ThirdTabHeadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    
   ClassifyHeadModel *c = [self.myArray objectAtIndex:indexPath.item];
  [cell.imgView sd_setImageWithURL:[NSURL URLWithString:c.banner_image_url]];
    
    //    NSLog(@"%@",c.banner_image_url);
    return cell;
}

//通知中心
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ClassifyHeadModel *c = [self.myArray objectAtIndex:indexPath.item];
    NSString *str = c.myId;
    NSString *str1 = c.posts_count;
   [[NSNotificationCenter defaultCenter]postNotificationName:@"push" object:nil userInfo:@{@"key":str,@"posts_count":str1}];
}


@end
